<?php

/*
	This file is part of Core. Copyright © 2007, Emanuele Vulcano.

	Core is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Core is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Core; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
	
*/

function CStr(&$x) {
	$res = null;
	
	if (is_string($x))
		return $x;
	
	if (is_object($x)) {
		if (method_exists($x, '__toString'))
			$res = $x->__toString();
	}
	
	if (is_null($res) && is_array($x)) {
		$y = 'array(';
		foreach ($x as $key => $val)
			$y .= "[$key] => " . CStr($val, $insight);
		$y .= ')';
		$res = $y;
	}
	
	if (is_null($res)) {
		$res = (string) $x;
		ob_start();
		var_export($res);
		$res = ob_get_clean();
	}
	
	return $res;
}

function CDump($v) {
	CDumpRef($v);
}

function CDumpToString(&$v) {
	ob_start(); var_dump($v); $x = ob_get_clean();
	return $x;
}

function CDumpRef(&$v) {
	ob_start(); var_dump($v); $x = ob_get_clean();
	CLog($x);
}

function CLog($what) {
	return CLogRef($what);
}

function &CLogRef(&$what) {
	error_log(CStr($what));
	return $what;
}

function CIsPHP4() {
	static $php4 = null;
	if (is_null($php4))
		$php4 = version_compare(PHP_VERSION, "5.0.0") < 1;
	return $php4;
}

function CSymbol($x) {
	if (CIsPHP4())
		return strtolower($x);
	return $x;
}

function CGetKey(&$from, $key) {
	if (is_array($from)) return $from[$key];
	if (is_object($from)) return $from->$key;
	return null;
}

function CIsMainFile($file) {
	return realpath($file) == realpath($_SERVER['SCRIPT_FILENAME']);
}

function CHasPrefix($string, $prefix, $caseSensitive = true) {
	if (!$caseSensitive) {
		$string = strtoupper($string);
		$prefix = strtoupper($prefix);
	}

	return @(substr($string, 0, strlen($prefix)) == $prefix) == true;
}

function CHasSuffix($string, $suffix, $caseSensitive = true
	) {
	if (!$caseSensitive) {
		$string = strtoupper($string);
		$suffix = strtoupper($suffix);
	}

	return @(substr($string, 0, strlen($suffix)) == $suffix) == true;
}

function CHasSubstring($string, $substring, $caseSensitive = true) {
	if (!$caseSensitive) {
		$string = strtoupper($string);
		$suffix = strtoupper($suffix);
	}

	$res = @(strpos($string, $substring));
	return $res !== null && $res !== false;
}

function CIsA(&$o, $trait) {
	if (!is_object($o))
		return false;
		
	if (method_exists($o, 'IsA'))
		return $o->IsA($trait);
	
	return is_a($o, $trait);
}

function CStaticAbstract($what, $class) {
	_CAbstractWhere($what, "static method of class $class");
}

function CAbstract($what, &$o) {
	$where = '';
	if (!is_null($o))
		$where = 'in object of class ' . get_class($o);
	_CAbstractWhere($what, $where);
}

function _CAbstractWhere($what, $where) {
	trigger_error("$what $where - this is an abstract method. Maybe it wasn't overridden?", E_USER_ERROR);
}

require_once 'CErrorFunctions.php';
require_once 'CInput.php';
require_once 'CTypeConstants.php';
require_once 'CXML.php';

?>